var div_u2b_panel='div_'+u2b+'_panel';
var div_u2b_panel_container=div_u2b_panel+'_container';

var ctrl_u2b='ctrl_'+div_u2b;
var button_get_u2b='btn_u2b';
var link_u2b='link_u2b';
var a_u2b_clearall='a_u2b_clearall';

function u2b_div_content(){
	
	$('<div id="'+ctrl_u2b+'"></div>').html(''+
			'<table width="100%"><tr><td width="100%" colspan="2" align="left">'+
			'	<b>add youtube link(s): <b>'+
			'</td></tr><tr><td align="left">'+
			'	<input id="'+link_u2b+'" type="text" style="width:300px; ">'+
			'	<input id="'+button_get_u2b+'" type="button" value="get u2b">'+
			'</td</tr>'+
			'</table>'+
		'</div>').appendTo('#'+div_u2b);
	
	$('#'+div_u2b).append('<br>'+
		'<table height="18"><tr><td width="100" >'+
			'<b>functions: </b><a id="'+a_u2b_clearall+'">clear all</a>'+
		'</td><td width="350" align="right">'+
			'<img id="u2b_progress" src="./images/progress.gif" />'+
		'</td></tr></table>');
	
	$('<div id="'+div_u2b_panel_container+'" class="container_div"></div>')
	.html('<div id="'+div_u2b_panel+'" class="sub_add_div"></div>')
	.appendTo('#'+div_u2b);

	$('#'+div_u2b_panel).droppable({
		accept: function(d){ 
	        if(d.attr("id").indexOf(TYPE_U2B_SLIDE)>=0){ 
	            return true;
	        }
		},
		drop:function(event,ui){
			var slide_div=ui.draggable.find('div');
			var slide_id=slide_div.attr('id');
			
			if (!(typeof(slide_id)===undefined_obj)){	
				addback_slide_to_panel(current_type,slide_id);
			}
			
		}
	});
	
	$('#'+button_get_u2b).click(function(){
		var url=$('#'+link_u2b).val();
		$('#u2b_progress').show('fast');
		if (url===null|| url===''){
			alert('input field is empty!');
			return;
		}else
			add_u2b_url(url);
		$('#u2b_progress').hide('fast');
	});
	
	$('#'+a_u2b_clearall).click(function(){
		clear_all(current_type);
	});
	
	/*
	$(function(){
		$('#'+form_u2b).ajaxForm({
		    beforeSend: function() {
		    },
		    uploadProgress: function(
		    		event, position, total, percentComplete) {
		    },
			complete: function(xhr) {
				var ret=xhr.responseText.split(splitter);
			}
		});
	});
	*/
	
	$('#u2b_progress').hide();
}

function add_u2b_url(url){
	var imgs=[];
	if(url===null){
		imgs=['','',''];
		return imgs; 
	}
	var results=url.match("[\\?&]v=([^&#]*)");
	var vid=(results===null)?url:results[1];
	imgs[0]='http://img.youtube.com/vi/'+vid+'/2.jpg';
	imgs[1]='http://img.youtube.com/vi/'+vid+'/0.jpg';
	
	$.getJSON('http://gdata.youtube.com/feeds/api/videos/'+
			vid+'?v=2&alt=json',function(data){
	    var title=data.entry.title.$t;
	    var desc=data.entry.media$group.media$description.$t;
	    var viewcount=data.entry.yt$statistics.viewCount;
	    var author=data.entry.author[0].name.$t;
	    var view_url='http://www.youtube.com/v/'+vid;

	    slide_data=imgs[0]+splitter+
	    		imgs[1]+splitter+
	    			"0"+data_splitter+
	    			"0"+data_splitter+
	    			"0"+data_splitter+
		    			title+inline_splitter+
			    		desc+inline_splitter+
			    		viewcount+inline_splitter+
			    		author+inline_splitter+
			    		view_url+
			    splitter+
			    "0"+splitter+
			    splitter;
	    
	    add_slide_to_panel(current_type,slide_data);
	});

	/*

	*/
}

function add_slide_to_panel(slide_type,data){
	var new_slide_id=slide_type+'_'+slide_idx;
	var e=data.split(splitter);
	var thumb_img=e[0];
	var exdata=slide_type+splitter+data+'';
	
	var new_slide_div=add_slide_div_all(
				new_slide_id, thumb_img, exdata);
	$('#div_'+slide_type+'_panel').prepend(new_slide_div);
	var array_type=select_array(slide_type);
	slides[array_type][new_slide_id]=new_slide_div;
	
	add_slide_properties(slide_type,new_slide_id,
					PROPERTIES_DRAG_INCLUDE);
	
	contact_session(TYPE_ADD,array_type,
					new_slide_id,new_slide_div);
	
	slide_idx++;
}

function addback_slide_to_panel(slide_type,slide_id){
	var added_div=(slides[ARRAY_SLIDES][slide_id].jquery?
			slides[ARRAY_SLIDES][slide_id].clone():
				slides[ARRAY_SLIDES][slide_id]);
	
	if (slides[ARRAY_SLIDES][slide_id] != null)
		delete slides[ARRAY_SLIDES][slide_id];
	$('#li_'+slide_id).remove();
	
	$('#div_'+slide_type+'_panel').append(added_div);
	var array_type=select_array(slide_type);
	slides[array_type][slide_id]=added_div;
	
	contact_session(TYPE_MOVEBACK,
				array_type,slide_id,null);
	
	add_slide_properties(slide_type,slide_id,
					PROPERTIES_DRAG_INCLUDE);
}

function add_slide_to_panel_auto(slide_type,id,content){
	$('#div_'+slide_type+'_panel').prepend(content);
	add_slide_properties(slide_type,id,
					PROPERTIES_DRAG_INCLUDE);
	var array_type=select_array(slide_type);
	slides[array_type][id]=content;
}

function add_slide_to_timeline(slide_type,drag_obj){
	var id=drag_obj.attr('id');
	$('#'+id).remove();

	/** to increase the width of timeline div if there are more slides
	 * than capability of timeslide window */
	if (Object.size(slides[ARRAY_SLIDES]) >= max_slides_on_line){
		var oldw=$('#'+div_timeline).width();
		$('#'+div_timeline).width(oldw+slide_width+additional_part);
	}
	
	var new_li_id='li_'+id;
	/*
	var thumb_img=drag_obj.find('img').attr('src');
	*/
	var thumb_img=drag_obj.css('background-image').replace(/\url|\(|\"|\"|\'|\)/g,'');
	/** for IE, Firefox */
	thumb_img=thumb_img.replace('\"','');
	var data=drag_obj.find('input').val();
	
	$('#'+dropped_slide_list).append(
		$('<li id="'+new_li_id+'" class="ui-state-default"></li>')
		.append(
			add_slide_div_all(id,thumb_img,data)
	));
	slides[ARRAY_SLIDES][id]=drag_obj;
	var array_type=select_array(slide_type);
	contact_session(TYPE_MOVE,array_type,id,null);
	
	if (slides[array_type][id] != null)
		delete slides[array_type][id];
	
	add_slide_properties(slide_type,id,
				PROPERTIES_DRAG_EXCLUDE);
}

function add_slide_to_timeline_auto(slide_type,id,content){
	var new_li_id='li_'+id;
	
	/** to increase the width of timeline div if there are more slides
	 * than capability of timeslide window */
	if (Object.size(slides[ARRAY_SLIDES]) >= max_slides_on_line){
		var oldw=$('#'+div_timeline).width();
		$('#'+div_timeline).width(oldw+slide_width+additional_part);
	}

	$('#'+dropped_slide_list).append(
		$('<li id="'+new_li_id+'" class="ui-state-default"></li>')
		.append(content));
	slides[ARRAY_SLIDES][id]=content;
	
	add_slide_properties(slide_type,id,
			PROPERTIES_DRAG_EXCLUDE);
}

function add_slide_div_all(new_slide_id,thumb_img,data){
	var pdata=$.trim(data);
	var type=pdata.split(splitter)[0];
	return ''+
	'<div id="'+new_slide_id+'" class="slide_info" style="background-image: url('+thumb_img+');">'+
	'<span id="slide_ctrl_'+new_slide_id+'" class="sub_control_slide_span">'+
	'<a><img src="./images/slidepreview.png" width="15px" alt="preview"></a>'+
	'<a id="a_'+new_slide_id+'" onclick="open_slide_editor(\''+new_slide_id+'\',\''+pdata+'\')"><img src="./images/slideedit.png" width="15px" alt="edit"></a>'+
	'<a onclick="remove_slide(\''+new_slide_id+'\',\''+type+'\');"><img src="./images/slideclose.png" width="15px" alt="close"></a>'+
	'</span>'+
	/*
	'<img src="'+thumb_img+'" width="'+width_img+'px" height="'+width_img+'px"/>'+
	*/
	'<input type="hidden" value="'+data+'">'+
	'</div>';
}

function remove_slide(id,type){
	var ctype=$('#li_'+id).length==1?ARRAY_SLIDES:type;
	var arraytype=select_array(ctype);
	if (ctype!=ARRAY_SLIDES){
		$('#'+id).remove();
	}else
		$('#li_'+id).remove();

	delete slides[arraytype][id];
	contact_session(TYPE_REMOVE,arraytype,id,null);
}

function clear_all(slide_type){
	var arraytype=select_array(slide_type);
	$.each(slides[arraytype],function(k,v){
		$('#'+k).remove();
	});
	$('#div_'+slide_type+'_panel').width(PANEL_WIDTH);
	$('#div_'+slide_type+'_panel').height(PANEL_HEIGHT);
	slides[arraytype]={};
	contact_session(TYPE_CLEAR,arraytype,null,null);

}